package com.blast.manager.adapter

import android.text.TextUtils
import android.view.View
import androidx.core.content.ContextCompat
import com.blast.manager.R
import com.chad.library.adapter.base.BaseViewHolder
import com.wxl.common.adapter.AbsKeyEventAdapter
import com.wxl.common.room.entity.LeiGuanEntity
import com.wxl.common.util.StringUtils

class NotMatchAdapter : AbsKeyEventAdapter<LeiGuanEntity> {

    var isChargeError = false
    var isBlastError = false
    val widths = ArrayList<Int>()

    constructor(
        data: List<LeiGuanEntity>?,
        isChargeError: Boolean,
        isBlastError: Boolean
    ) : super(R.layout.item_delay_list_layout, data) {
        this.isChargeError = isChargeError
        this.isBlastError = isBlastError
    }

    fun setWidths(widths: ArrayList<Int>) {
        this.widths.clear()
        this.widths.addAll(widths)
    }

    override fun convert(helper: BaseViewHolder, item: LeiGuanEntity) {
        super.convert(helper, item)
        if(this.widths.isNotEmpty()){
            setViewWidth(helper.getView(R.id.itemDelayNumberText)
                ,helper.getView(R.id.itemDelayCodeText)
                ,helper.getView(R.id.itemDelayTimeText)
                ,helper.getView(R.id.itemDelaySortText)
                ,helper.getView(R.id.itemDelayKongText)
                ,helper.getView(R.id.itemDelayCjqText))
        }
        helper.setText(R.id.itemDelayNumberText, (helper.adapterPosition + 1).toString())
        helper.setText(R.id.itemDelayTimeText, item.delay)
        helper.setText(R.id.itemDelayCodeText, StringUtils.getUid(item.UID))
        if (!TextUtils.isEmpty(item.holeNumber)) {
            val hexString: String = StringUtils.HexToBin16(item.holeNumber)
            helper.setText(R.id.itemDelaySortText, StringUtils.getRowNumber(hexString).toString())
            helper.setText(R.id.itemDelayKongText, StringUtils.getHoleNumber(hexString).toString())
        } else {
            helper.setText(R.id.itemDelaySortText, "")
            helper.setText(R.id.itemDelayKongText, "")
        }
        helper.setTextColor(
            R.id.itemDelayCjqText,
            ContextCompat.getColor(mContext, android.R.color.holo_red_dark)
        )
        if (isBlastError) {
            helper.setTextColor(
                R.id.itemDelayNumberText,
                ContextCompat.getColor(mContext, android.R.color.holo_red_dark)
            )
            helper.setTextColor(
                R.id.itemDelayCodeText,
                ContextCompat.getColor(mContext, android.R.color.holo_red_dark)
            )
            helper.setTextColor(
                R.id.itemDelaySortText,
                ContextCompat.getColor(mContext, android.R.color.holo_red_dark)
            )
            helper.setTextColor(
                R.id.itemDelayKongText,
                ContextCompat.getColor(mContext, android.R.color.holo_red_dark)
            )
            helper.setTextColor(
                R.id.itemDelayCjqText,
                ContextCompat.getColor(mContext, android.R.color.holo_red_dark)
            )
            showBlastError(helper, item)
        } else if (isChargeError) {
            if (!TextUtils.isEmpty(item.state) && TextUtils.equals(
                    "0",
                    item.state.toCharArray()[0].toString()
                )
            ) {
                helper.setText(R.id.itemDelayCjqText, "充电异常")
            } else {
                helper.setText(R.id.itemDelayCjqText, "")
            }
        } else {
            if (!TextUtils.isEmpty(item.state) && TextUtils.equals(
                    "0",
                    item.state.toCharArray()[6].toString()
                )
            ) {
                helper.setText(R.id.itemDelayCjqText, "漏接")
            } else if (!TextUtils.isEmpty(item.state) && TextUtils.equals(
                    "0",
                    item.state.toCharArray()[5].toString()
                )
            ) {
                helper.setText(R.id.itemDelayCjqText, "多接")
            } else {
                helper.setText(R.id.itemDelayCjqText, "")
            }
        }
    }


    private fun setViewWidth(vararg vs: View) {
        (0 until vs.size).forEach { i ->
            vs[i].layoutParams.width = widths[i]
        }
    }

    private fun showBlastError(helper: BaseViewHolder, item: LeiGuanEntity) {
        if (!TextUtils.isEmpty(item.state) && TextUtils.equals(
                "0",
                item.state.toCharArray()[4].toString()
            )
        ) {
            helper.setText(R.id.itemDelayCjqText, "电压异常")
        } else if (!TextUtils.isEmpty(item.state) && TextUtils.equals(
                "0",
                item.state.toCharArray()[1].toString()
            )
        ) {
            helper.setText(R.id.itemDelayCjqText, "密码异常")
        } else if (!TextUtils.isEmpty(item.state) && TextUtils.equals(
                "0",
                item.state.toCharArray()[3].toString()
            )
        ) {
            helper.setText(R.id.itemDelayCjqText, "充电异常")
        } else {
            helper.setText(R.id.itemDelayCjqText, "")
        }
    }


}